package com.example.home.film.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.film.FilmDetailActivity;
import com.example.home.film.bean.FilmListBean;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class FilmListAdapter extends RecyclerView.Adapter<FilmListAdapter.MyHoldy> {
    private Context context;
    private List<FilmListBean.RowsBean> list;
    private boolean showAllData = false;

    public void setShowAllData(boolean showAllData) {
        this.showAllData = showAllData;
        notifyDataSetChanged(); // 刷新 RecyclerView 以显示所有数据
    }

    public FilmListAdapter(Context context, List<FilmListBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setFilmList(List<FilmListBean.RowsBean> filmList) {
        this.list = filmList;
    }


    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_filist, null);
        return new FilmListAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        FilmListBean.RowsBean rowsBean = list.get(position);
        Glide.with(context).load(ip + rowsBean.getCover()).into(holder.cover);
        holder.name.setText(rowsBean.getName());
        holder.playDate.setText("上映时间："+rowsBean.getName());
        holder.duration.setText("电影时长："+rowsBean.getDuration());
        holder.score.setText("评分："+rowsBean.getScore() + "分");
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, FilmDetailActivity.class);
                intent.putExtra("filmId",String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return showAllData ? list.size() : Math.min(list.size(), 5);
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private ImageView cover;
        private TextView name;
        private TextView playDate;
        private TextView duration;
        private TextView score;
        private RelativeLayout rootView;


        public MyHoldy(@NonNull View itemView) {
            super(itemView);

            cover = itemView.findViewById(R.id.cover);
            name = itemView.findViewById(R.id.name);
            playDate = itemView.findViewById(R.id.playDate);
            duration = itemView.findViewById(R.id.duration);
            score = itemView.findViewById(R.id.score);
            rootView = itemView.findViewById(R.id.rootView);
        }
    }
}
